package com.zyf.lj.service.impl;

import com.zyf.lj.entity.JobInfo;
import com.zyf.lj.mapper.JobInfoMapper;
import com.zyf.lj.service.JobInfoService;
import com.zyf.lj.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 招聘信息服务实现类
 */
@Service
public class JobInfoServiceImpl implements JobInfoService {
    
    @Autowired
    private JobInfoMapper jobInfoMapper;
    
    @Override
    @Transactional
    public boolean addJobInfo(JobInfo jobInfo) {
        // 设置创建时间和更新时间
        Date now = new Date();
        jobInfo.setCreateTime(now);
        jobInfo.setUpdateTime(now);
        jobInfo.setIsDeleted(0);
        
        // 插入数据
        int rows = jobInfoMapper.insert(jobInfo);
        return rows > 0;
    }
    
    @Override
    @Transactional
    public boolean updateJobInfo(JobInfo jobInfo) {
        // 设置更新时间
        jobInfo.setUpdateTime(new Date());
        
        // 更新数据
        int rows = jobInfoMapper.update(jobInfo);
        return rows > 0;
    }
    
    @Override
    public JobInfo getJobInfoById(Integer id) {
        JobInfo jobInfo = jobInfoMapper.selectById(id);
        if (jobInfo != null) {
            // 处理标签和要求
            processJobInfo(jobInfo);
        }
        return jobInfo;
    }
    
    @Override
    @Transactional
    public boolean deleteJobInfoById(Integer id) {
        int rows = jobInfoMapper.deleteById(id);
        return rows > 0;
    }
    
    @Override
    public List<JobInfo> getAllJobInfos() {
        List<JobInfo> jobInfos = jobInfoMapper.selectAll();
        // 处理标签和要求
        jobInfos.forEach(this::processJobInfo);
        return jobInfos;
    }
    
    @Override
    public PageResult<JobInfo> getJobInfoPage(Integer pageNum, Integer pageSize, String keyword, String industry, String jobType, String orderBy, String orderType) {
        // 计算偏移量
        int offset = (pageNum - 1) * pageSize;
        
        // 默认排序
        if (!StringUtils.hasText(orderBy)) {
            orderBy = "publish_date";
        }
        if (!StringUtils.hasText(orderType)) {
            orderType = "desc";
        }
        
        // 查询数据
        List<JobInfo> jobInfos = jobInfoMapper.selectPage(offset, pageSize, keyword, industry, jobType, orderBy, orderType);
        
        // 处理标签和要求
        jobInfos.forEach(this::processJobInfo);
        
        // 查询总数
        int total = jobInfoMapper.countJobs(keyword, industry, jobType);
        
        // 返回分页结果
        return new PageResult<>(pageNum, pageSize, total, jobInfos);
    }
    
    @Override
    public List<JobInfo> getLatestJobInfos(Integer limit) {
        List<JobInfo> jobInfos = jobInfoMapper.selectLatest(limit);
        // 处理标签和要求
        jobInfos.forEach(this::processJobInfo);
        return jobInfos;
    }
    
    /**
     * 处理招聘信息的标签和要求
     * @param jobInfo 招聘信息对象
     */
    private void processJobInfo(JobInfo jobInfo) {
        // 处理标签
        if (StringUtils.hasText(jobInfo.getTags())) {
            List<String> tagList = Arrays.stream(jobInfo.getTags().split(","))
                .map(String::trim)
                .filter(StringUtils::hasText)
                .collect(Collectors.toList());
            jobInfo.setTagList(tagList);
        }
        
        // 处理要求
        if (StringUtils.hasText(jobInfo.getRequirements())) {
            List<String> requirementList = Arrays.stream(jobInfo.getRequirements().split("\n"))
                .map(String::trim)
                .filter(StringUtils::hasText)
                .collect(Collectors.toList());
            jobInfo.setRequirementList(requirementList);
        }
    }
} 